package com.kingyea.kim.trip.service.impl;

import com.kingyea.kim.base.ErrorMsg;
import com.kingyea.kim.base.Result;
import com.kingyea.kim.trip.bean.Trip;
import com.kingyea.kim.trip.condition.TripCondition;
import com.kingyea.kim.trip.dao.TripMapper;
import com.kingyea.kim.trip.service.TripService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lgy
 * @version v1.0.0
 * @description
 * @date Created in 2018/8/20 0020 16:26
 */
@Service
public class TripServiceImpl implements TripService {
    //打印日志
    private final static Logger logger = LoggerFactory.getLogger(TripServiceImpl.class);

    @Autowired
    private TripMapper tripMapper;
    /**
     *
     * 列表显示，可以通过标题和时间获取对象
     */
    @Override
    public Result list(TripCondition condition) {
        Result result=new Result();
        try {
            List returnList = new ArrayList();
            //待办日程数
            int total=tripMapper.selectCount();
            Map<String,Integer> extraMap=new HashMap<String,Integer>();
            List<Trip> tripList=tripMapper.selectByCondition(condition);
            extraMap.put("total",tripList.size());
            result.setExtra(extraMap);
            // TODO
                List<String> tripDateList=tripMapper.selectTripDate(condition);//获取日期列表
                if(!CollectionUtils.isEmpty(tripDateList)&&tripDateList.size()>0){
                    condition.setTripDateList(tripDateList);
                    List<Trip> list = tripMapper.selectByTripDate(condition);//根据日期查询对象
                    if(!CollectionUtils.isEmpty(list)&&list.size()>0){
                        Map<Date, List<Trip>> map = list.stream().collect(Collectors.groupingBy(Trip::getTripDate));
                        Map<Date, List<Trip>> map2 = new TreeMap<>();
                        map.entrySet().stream()
                                .sorted(Map.Entry.<Date, List<Trip>>comparingByKey()
                                        .reversed()).forEachOrdered(e -> map2.put(e.getKey(), e.getValue()));
                        for(Date tripDate : map2.keySet()){
                            Map<String,Object> returnMap = new HashMap<>();
                            returnMap.put("time",tripDate);
                            returnMap.put("data",map.get(tripDate));
                            returnList.add(returnMap);
                        }
                    }
                }
            result.setCodeData(returnList);
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            result.setCode(ErrorMsg.SYSTEM_ERROR.getCode());
        }
        return result;
    }

//    @Override
//    public Result selectByCondition(TripCondition condition) {
//        Result result=new Result();
//        try {
//            //待办日程数
//            int total=tripMapper.selectCount();
//            Map<String,Object> map=new HashMap<String,Object>();
//            map.put("total",total);
//            // TODO
//            List<Trip> list=tripMapper.selectByCondition(condition);
//            result.setCodeData(list);
//            result.setExtra(map);
//        }catch (Exception e){
//            logger.error(e.getMessage());
//            e.printStackTrace();
//            result.setCode(ErrorMsg.SYSTEM_ERROR.getCode());
//        }
//        return result;
//    }
    /**
     * 添加日程
     */
    @Override
    public Result addTrip(TripCondition trip) {
        Result result=new Result();
        try {
            if(trip.getTripUserid()!=null) {//添加日程必须有日程人
                result.setCount(tripMapper.addTrip(trip));
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            result.setCode(ErrorMsg.SYSTEM_ERROR.getCode());
        }
        return result;
    }

    /**
     *
     * 修改日程状态或者日程信息
     */
    @Override
    public Result update(TripCondition tripCondition) {
        Result result=new Result();
        try{
            if (tripCondition.getId()!=null){//要将修改的日程Id传过来
                Long count=tripMapper.update(tripCondition);
                result.setCount(count);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            result.setCode(ErrorMsg.SYSTEM_ERROR.getCode());
        }
        return result;
    }

    /**
     *
     * 删除日程
     */
    @Override
    public Result deleteById(Integer id) {
        Result result=new Result();
        try {
          if(tripMapper.selectById(id)!=null){
              Integer c=tripMapper.deleteById(id);
              result.setExtra(c);
          }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            result.setCode(ErrorMsg.SYSTEM_ERROR.getCode());
        }
        return  result;
    }

    /**
     *
     * 根据ID查找对象
     */
    @Override
    public Result selectById(Integer id) {
        Result result=new Result();
        try {
            if(id!=null){
                result.setModel(tripMapper.selectById(id));
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            result.setCode(ErrorMsg.SYSTEM_ERROR.getCode());
        }
        return result;
    }

}
